package com.atnjupt.crud.service;

import com.atnjupt.crud.bean.Employee;
import com.atnjupt.crud.bean.EmployeeExample;
import com.atnjupt.crud.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Wizarder
 * @create 2021-08-29 16:05
 */
@Service
public class EmployeeSerivice {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 查询所有员工
     * @return 员工集合
     */
    public List<Employee> getAll() {
       return employeeMapper.selectByExampleWithDept(null);
    }

    /**
     * 保存员工
     * @param employee  已经封装好的用户对象
     */
    public void saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    /**
     * 核验用户名是否已经存在
     * @return true存在，false不存在
     */
    public Boolean checkEmpName(String empName) {
        //创建一个员工判别例子
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count  = employeeMapper.countByExample(employeeExample);
        //如果返回的数目不为0，说明存在该用户名
        if (count != 0){
            return true;
        }else{
            //为0，说明没有该用户名
            return false;
        }
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    public Employee getEmp(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    /**
     * 更新员工信息
     * @param employee 更新的员工
     * @return
     */
    public int updateEmp(Employee employee) {
        //有选择的更新
        int update = employeeMapper.updateByPrimaryKeySelective(employee);
        return update;
    }

    /**
     * 根据id删除员工
     * @param id
     * @return
     */
    public int deleteEmp(Integer id) {
        int count = employeeMapper.deleteByPrimaryKey(id);
        return count;
    }

    /**
     * 批量id删除员工
     * @param id
     * @return
     */
    public int deleteBatch(List<Integer> id) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpIdIn(id);
        // delete from tbl_employee where emp_id in (1,2,...)
        int count = employeeMapper.deleteByExample(employeeExample);
        return count;
    }
}
